package com.company.bookstore.controller.cart_module;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.bookstore.common.formatUtils.FormatUtils;
import com.company.bookstore.common.result.MvcResult;
import com.company.bookstore.entities.book_module.Book;
import com.company.bookstore.entities.book_module.BookImg;
import com.company.bookstore.entities.cart_module.Cart;
import com.company.bookstore.service.book_module.BookImgService;
import com.company.bookstore.service.book_module.BookService;
import com.company.bookstore.service.cart_module.CartService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 江洪锦
 * @since 2020-09-18
 */
@Api(tags = "系统：购物车")
@Slf4j
@RestController
@RequestMapping("/api/cart")
public class CartController {

    @Resource
    CartService cartService;

    @Resource
    BookService bookService;

    @Resource
    BookImgService bookImgService;

    /**
     * 根据userId查询购物车列表
     * @param userId cart表的userId
     * @return List<Cart>
     */
    @PostMapping("/getCartListByUserId")
    public MvcResult<Object> getCartListByUserId(@RequestBody Cart cart){
        List<Cart> cartList = cartService.list(new QueryWrapper<Cart>()
                .eq("user_id",cart.getUserId()));
        if(!cartList.isEmpty()){
            for(Cart cartOne:cartList){
//          cartOne.setBook(bookService.getById(cartOne.getBookId()));
                Book book = bookService.getById(cartOne.getBookId());
                book.setBookImgs(bookImgService.list(new QueryWrapper<BookImg>()
                        .eq("fk_book_id",cartOne.getBookId())));
                book = FormatUtils.formatOneImgUrl(book);
                cartOne.setBook(book);
//            cartOne.getBook().setBookImgs(bookImgService.list(new QueryWrapper<BookImg>()
//                    .eq("fk_book_id",cartOne.getBookId())));
            }
            MvcResult.SELECT_SUCCESS.setObject(cartList);
            return MvcResult.SELECT_SUCCESS;
        }
        else {return MvcResult.builder().code(200).message("该用户购物车列表为空").build();}
    }


    /**
     * 新增加购物车
     * @param userId
     * @param bookId
     * @param num
     * @return MvcResult
     */
    @PostMapping("/addCart")
    public MvcResult<Object> addCart(@RequestBody Cart cart){
        int count = cartService.getLastCartId();
        cart.setId(++count);
        if(cart.getNum() > 3){
            return MvcResult.builder().code(500).message("购买图书数量大于三本").build();
        }
        if(cartService.save(cart)){
            return MvcResult.builder().code(200).message("添加成功").build();
        }
        return MvcResult.builder().code(500).message("请检查参数列表").build();
    }

    /**
     * 更新购物车
     * @param ids:[] 需要更新的cart表的ID列表
     * @param nums:[] 需要更新的cart表的图书数量列表
     * @return MvcResult
     */
    @PutMapping("/updateByIdsAndNums")
    @Transactional(rollbackFor = {Exception.class})
    public MvcResult<Object> updateCart(@RequestBody Map<String,List> map){
        List<Cart> cartList = cartService.listByIds(map.get("ids"));
        List<Integer> numList = map.get("nums");
        for(Cart cartOne:cartList){
            cartOne.setNum(numList.get(cartList.indexOf(cartOne)));
        }
        if(cartService.updateBatchById(cartList)){
            return MvcResult.builder().code(200).message("更新成功").build();
        }
        else {return MvcResult.builder().code(500).message("更新失败，请检查参数列表").build();}
    }

    /**
     * 删除购物车表
     * @param ids:[]
     * @return MvcResult
     */
    @DeleteMapping("/deleteCartByIds")
    public MvcResult<Object> deleteCarByIds(@RequestBody Map<String,List> map){
        if(cartService.removeByIds(map.get("ids"))){
            return MvcResult.builder().code(200).message("删除成功").build();
        }
        else {return MvcResult.builder().code(500).message("该购物车表不存在").build();}
    }
}

